//
//  PreferenceController.m
//  Tables
//
//  Created by mathias Muller on 9/13/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "PreferenceController.h"
#import "EasyChallenge.h"

@implementation PreferenceController
@synthesize operatorControl,randomControl,lengthControl;
@synthesize control1,control2, control3, control4, control5, control6, control7, control8, control9;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
	}
	return self;
}

- (void)awakeFromNib {
	//set operator
	NSString *prefOp = [EasyChallenge getOperator];
	NSInteger index = 0;
	if([prefOp  isEqualToString: @"x"] ) {
		index = 0;
	} else if([prefOp  isEqualToString:@"+"]) {
		index = 1;
	} else if([prefOp  isEqualToString:@"-"]) {
		index = 2;
	} else if([prefOp  isEqualToString:@"/"] ) {
		index = 3;
	}
	[operatorControl selectSegment:index];
	//Random
	[randomControl setOn:[EasyChallenge isRandom]];
	[lengthControl setEnabled:[EasyChallenge isRandom]];
	[lengthControl setValue:[[NSUserDefaults standardUserDefaults] integerForKey:@"LENGTH"]];
	int i;
	for (i=1;i<10; i++) {
		NSNumber * index = [NSNumber numberWithInt:i];
		NSString * selectorStr = [NSString stringWithFormat:@"control%@",index];
		UISwitch * s = [self performSelector:NSSelectorFromString(selectorStr)];
		[s setOn:[[NSUserDefaults standardUserDefaults] integerForKey:[index stringValue]]];
	}
}

-(IBAction)setOperator:(id)sender {
	UISegmentedControl * o = (UISegmentedControl * ) sender;
	[EasyChallenge setOperator:[o titleForSegmentAtIndex:o.selectedSegmentIndex]];
}

-(IBAction)setRamdom:(id)sender {
	UISwitch * s = (UISwitch *)sender;
	[EasyChallenge setRandom:[s isOn]];
	[lengthControl setEnabled:[s isOn]];
}

-(IBAction)setLength:(id)sender {
	UISlider * s = (UISlider *)sender;
	[[NSUserDefaults standardUserDefaults]  setInteger:[s value] forKey:@"LENGTH"];
}

-(IBAction)setNumbers:(id)sender {
	UISwitch * s = (UISwitch *)sender;
	NSString * key = nil;
	if(s == self.control1) {
		key = @"1";
	} else if(s == self.control2) {
		key = @"2";
	} else if(s == self.control3) {
		key = @"3";
	} else if(s == self.control4) {
		key = @"4";
	} else if(s == self.control5) {
		key = @"5";
	} else if(s == self.control6) {
		key = @"6";
	} else if(s == self.control7) {
		key = @"7";
	} else if(s == self.control8) {
		key = @"8";
	} else if(s == self.control9) {
		key = @"9";
	}
		
	[[NSUserDefaults standardUserDefaults]  setInteger:[s isOn] forKey:key];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}


@end
